<?php
class PagesController extends AppController {

	var $name = "Pages";
	var $uses = array('Question', 'News', 'Upload');
	var $helpers = array('Html', 'Form');

	function beforeFilter() {
		parent::beforeFilter();

		$this->Auth->authorize = 'controller';
	}

	function isAuthorized() {
		if( $this->action == 'admin_index' ) {
			if($this->Auth->user('role') != 'a') {
				return false;
			}
			else {
				return true;
			}
		}
	}

	/**
	 * Inicio del panel de admin
	 *
	 */
	function admin_index() {
		$this->pageTitle = 'Administrar';
		
		$questions = $this->Question->query('SELECT COUNT(Answer.id) as no_answer FROM questions as Question JOIN answers as Answer ON (Question.id = Answer.question_id)');
		$questions = $this->Question->find('count') - $questions[0][0]['no_answer'];
		
		$news = $this->News->find('all', array(
			'order' => 'News.created DESC',
			'limit' => 5
		));
		
		$uploads = $this->Upload->find('all', array(
			'order' => 'Upload.created DESC',
			'limit' => 5
		));
		
		$this->set('questions_count', $questions);
		$this->set('news', $news);
		$this->set('uploads', $uploads);
	}
}
?>